package view;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionListener;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import controller.ActionLogin;
import controller.EditAccountKeyListener;
import controller.Engine;

@SuppressWarnings("serial")
public class Login extends JFrame{

	JPanel contenitore = new JPanel();
	JPanel username = new JPanel();
	JLabel user = new JLabel("Username");
	static JTextField usertxt = new JTextField("prova",20);
	JPanel password = new JPanel();
	JLabel pass = new JLabel("Password");
	static JPasswordField  passtxt = new JPasswordField("prova",20);
	JPanel buttons = new JPanel();
	JButton login_button = new JButton();
	JButton registra_button = new JButton();
	KeyListener txtcontrol = new EditAccountKeyListener();

	static JFrame login = new JFrame ("Login");

	public Login() {
		login=this;
		login.setTitle("Login");
		login.setIconImage(Engine.immaginelog);
		contenitore.setBackground(Color.white);
		ActionListener action = new ActionLogin();

		user.setPreferredSize(new Dimension (70,30));
		pass.setPreferredSize(new Dimension (70,30));

		login_button.addActionListener(action);
		login_button.setName("login");
		login_button.setText("Login");
		login_button.setMinimumSize(new Dimension (100,27));
		login_button.setPreferredSize(new Dimension (100,27));
		login_button.setMaximumSize(new Dimension (100,27));
		registra_button.addActionListener(action);
		registra_button.setName("registra");
		registra_button.setText("Registrati");
		registra_button.setMinimumSize(new Dimension (100,27));
		registra_button.setPreferredSize(new Dimension (100,27));
		registra_button.setMaximumSize(new Dimension (100,27));

		buttons.setLayout(new BoxLayout(buttons, BoxLayout.X_AXIS));
		buttons.add(login_button);
		buttons.add(Box.createHorizontalStrut(20));
		buttons.add(registra_button);
		buttons.setBackground(Color.white);

		username.add(user);
		username.add(usertxt);
		username.setName("username");
		username.addKeyListener(txtcontrol);
		username.setBackground(Color.white);

		password.add(pass);
		password.add(passtxt);
		password.setName("password");
		password.addKeyListener(txtcontrol);
		password.setBackground(Color.white);

		contenitore.setLayout(new BoxLayout(contenitore, BoxLayout.Y_AXIS));
		contenitore.setAlignmentY(CENTER_ALIGNMENT);
		contenitore.setAlignmentX(CENTER_ALIGNMENT);
		contenitore.add(Box.createVerticalStrut(8));
		contenitore.add(username);
		contenitore.add(password);
		contenitore.add(Box.createVerticalStrut(8));
		contenitore.add(buttons);
		contenitore.add(Box.createVerticalStrut(22));
		add(contenitore);

		login.add(contenitore);

		//posizionatore del login frame
		login.setResizable(false);
		login.setPreferredSize(new Dimension (350,160));
		login.pack();
		Dimension schermo = Toolkit.getDefaultToolkit().getScreenSize();
		int x = (schermo.width-350)/2;
		int y = (int) ((schermo.height-160)/1.3);
		login.setLocation(x,y);
		login.requestFocus();



		this.addWindowListener(new WindowAdapter()
		{
			@Override
			public void windowClosing(WindowEvent e)
			{
				Engine.closeDB();
				System.exit(0);
			}
		});
		setVisible(true);
	}

	@SuppressWarnings("deprecation")
	public static void validateLogin(){
		Engine.validateLogin(usertxt.getText(),passtxt.getText());
	}

	public static void restart() {
		login.setEnabled(true);
		login.setVisible(true);
	}

	public static void restart(String user,String pass) {
		login.setEnabled(true);
		login.setVisible(true);
		usertxt.setText(user);
		passtxt.setText(pass);
	}

	public static void pauseLogin() {
		login.setEnabled(false);
	}

	public static void stopLogin(){
		SplashScreen.disableSplash();
		login.dispose();
	}

	public static void allertInvalidUsernameOrPassword() {
		JOptionPane.showMessageDialog(new JFrame(),"Username o Password errata o inesistente");
	}



}
